package xuanzhen.pro.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Service;

import xuanzhen.pro.dao.RelayMapper;
import xuanzhen.pro.dao.WxUserMsgMapper;
import xuanzhen.pro.pojo.Relay;
import xuanzhen.pro.service.RelayService;
import xuanzhen.wx.pojo.WxUserMsg;
@Service
public class RelayServiceImpl implements RelayService {
	@Resource
	RelayMapper relayMapper;
	@Resource
	WxUserMsgMapper wxUserMapper;
	@Override
	public String addRelay(Relay relay) throws Exception {
		
	/*	long timestamp = System.currentTimeMillis() / 1000;
		String noncestr = RandomStringUtils.randomAlphanumeric(4);
		relay.setRelayNo(timestamp+noncestr);*/
		// 设置创建时间
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
		String nowTime = dateFormat.format(now);
		relay.setCreateTime(nowTime);
		relay.setRelayState("normal");
		relayMapper.addRelay(relay);
		
		Relay r= relayMapper.selectRelayByNo(relay.getRelayNo());
		if(r!=null){
			return r.getRelayNo();
		}
		return r.getRelayNo();
	}
	@Override
	public void acceptRelay(Relay relay) throws Exception {
		// TODO Auto-generated method stub
		//判定openid是否相同
		//判定接力单状态符合接单要求和时间过期与否
		//判定接收人
		//线程.控制接单
		//提交时判断接力单状态并给出提示控制
	}
	@Override
	public Relay selectRelayByRelayNo(String relayNo) throws Exception {
		Relay relay= relayMapper.selectRelayByRelayNo(relayNo);
		return relay;
	}
	@Override
	public Relay updateRelay(Relay relay) throws Exception {
		relayMapper.updateRelay(relay);
		Relay r= relayMapper.selectRelayByNo(relay.getRelayNo());
		return relay;
	}
	@Override
	public void saveWxUser(WxUserMsg user) throws Exception {
		wxUserMapper.saveWxUser(user);
		
	}
	@Override
	public WxUserMsg selectWxUserByOpenid(String openid) throws Exception {
		WxUserMsg wx =wxUserMapper.selectWxUser(openid);
		return wx;
	}
	@Override
	public List<Relay> selectAll() throws Exception {
		List<Relay> relayList = relayMapper.selectAll();
		return relayList;
	}
	@Override
	public List<Relay> selectByPageNo(int pageNo) throws Exception {
		int recordNo = pageNo*10;
		return relayMapper.selectByPageNo(recordNo);
	}
	@Override
	public int countRecords() throws Exception {
		int count = relayMapper.countRecords();
		if(count%10==0){
			count = count/10;
		}else{
			count = count/10+1;
		}
		return count;
	}

}
